package com.demo.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.demo.dao.BaseDao;
import com.demo.entity.User;
import com.demo.service.MyService;

/**
 * @author: demo
 * 
 * @Date: 12-10-31
 * @Time: 下午5:27
 */
@Service
public class MyServiceImpl implements MyService {
	
	protected Logger logger = Logger.getLogger(MyServiceImpl.class);
	
	@Resource
	private BaseDao baseDao;

	/**
	 * 事务被回滚
	 */
	@Override
	public Integer insert(User user){
		Integer num = 0;
		try {
			num = baseDao.insert("userMapper.insertUser", user);
			//测试springAOP，这里故意爆出漏洞！;
//			num = baseDao.insert("userMapper.insertUser", user);
		} catch (Exception e) {
			logger.error("insert",e);
			throw new RuntimeException("错误，测试springAOP");
		}

		return num;

	}

	/**
	 * 事务被回滚
	 */
	@Override
	public Integer insert2(User user) throws Exception{
		Integer num = 0;
		try {
			user.setUserId(Long.valueOf("777"));
			num = baseDao.insert("userMapper.insertUser", user);
			user.setUsername("insert2方法中测试springAOP，这里故意爆出漏洞！");
			 baseDao.insert("userMapper.insertUser", user);
		} catch (Exception e) {
			logger.error(e);
			throw e;
		}

		return num;

	}
	@Override
	public User getUser(User user) {
		
		logger.info("user.email = " + user.getEmail());
		return (User) baseDao.queryForObject("userMapper.getUserByEmail", user);
	}
	@Override
	public User getUserByEmail(String email) {
		logger.info("user.email = " + email);
		return  (User)baseDao.queryForObject("userMapper.getUserByEmail",email);
	}
	
	@SuppressWarnings("unchecked")
	@Cacheable(value="baseCache")
	@Override
	public List<User> selectList() {
		logger.info("没使用ehcache缓存，使用数据库查询.............");
		return (List<User>) baseDao.selectList("userMapper.getUserList");
	}
	
	@Override
	public int updateUser(User user) {
		return baseDao.updateObject("userMapper.updateUser", user);
	}
	@Override
	public int deleteUser(String userId) {
		return baseDao.updateObject("userMapper.updateUser", userId);
	}

}
